package com.kichina.parking.upstream;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kichina.parking.controller.parking.handler.CarBanDownControlHandler;
import com.kichina.parking.exception.extend.JsonParseException;
import com.kichina.parking.service.logic.bean.StationaryCarRecord;
import com.kichina.parking.service.logic.bean.WhitelistInfoRecord;
import com.kichina.parking.service.logic.service.StationaryCarRecordService;
import com.kichina.parking.service.logic.service.WhitelistInfoRecordService;
import com.kichina.parking.utils.JsonToEntityUtil;
import com.kichina.parking.utils.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.Date;
import java.util.Objects;

/**
 * @author Surging
 * @version 1.0
 * @ClassName MonthPayUpstreamHandler
 * @date 2022-09-14 20:03
 * @desc 固定车记录上传
 */
@Component
@Slf4j
public class Parking14StationaryCarUpstreamHandler {

    private final StationaryCarRecordService stationaryCarRecordService;


    private final WhitelistInfoRecordService whitelistInfoRecordService;

    public Parking14StationaryCarUpstreamHandler(StationaryCarRecordService stationaryCarRecordService, WhitelistInfoRecordService whitelistInfoRecordService) {
        this.stationaryCarRecordService = stationaryCarRecordService;
        this.whitelistInfoRecordService = whitelistInfoRecordService;
    }

    /**
     * @param jsonObject 数据实体
     * @author Surging
     * @Description 固定车上传结果处理(增删改)
     * @Return void
     * @date 20:53 2022-09-15
     **/
    public void recordParkingStationaryCarUpstream(JSONObject jsonObject) throws JsonParseException {

        StationaryCarRecord stationaryCarRecord;

        try {
            stationaryCarRecord = JsonToEntityUtil.jsonToEntity(jsonObject, StationaryCarRecord.class);
            //添加
            stationaryCarRecord.setStationaryCarId(UUIDUtil.getUUID());
            stationaryCarRecord.setUpdateTime(new Date());
            //原实体有createTime字段，serviceCreateTime作区分
            stationaryCarRecord.setCreateTime(new Date());
            stationaryCarRecordService.save(stationaryCarRecord);
            //处理不成功不做处理
            if (!stationaryCarRecord.getMessage().contains("成功")) {
                return;
            }
            if (Objects.nonNull(stationaryCarRecord.getUuid())) {
                WhitelistInfoRecord whitelistInfoRecord = whitelistInfoRecordService.getOne(new QueryWrapper<WhitelistInfoRecord>()
                        .lambda().eq(WhitelistInfoRecord::getUuid, stationaryCarRecord.getUuid())
                        .eq(WhitelistInfoRecord::getParkid, stationaryCarRecord.getParkid())
                        .last("limit 1"));

                if (Objects.nonNull(whitelistInfoRecord)) {
                    //删除操作
                    if (whitelistInfoRecord.getOperateType() == 3) {
                        whitelistInfoRecordService.removeById(whitelistInfoRecord.getWhitelistInfoRecordId());
                        return;
                    }
                } else {
                    if (Objects.nonNull(stationaryCarRecord.getUuid())) {
                        whitelistInfoRecord = whitelistInfoRecordService.getOne(new QueryWrapper<WhitelistInfoRecord>()
                                .lambda().eq(WhitelistInfoRecord::getCardId, stationaryCarRecord.getCardId())
                                .eq(WhitelistInfoRecord::getParkid, stationaryCarRecord.getParkid())
                                .last("limit 1"));
                    }

                    if (Objects.nonNull(stationaryCarRecord.getUuid()) &&
                            Objects.nonNull(CarBanDownControlHandler.infoRecords.get(stationaryCarRecord.getUuid()))) {
                        //更新操作
                        WhitelistInfoRecord whitelistInfoRecord1 = CarBanDownControlHandler.infoRecords.get(stationaryCarRecord.getUuid());
                        String whitelistInfoRecordId = whitelistInfoRecord.getWhitelistInfoRecordId();
                        BeanUtils.copyProperties(whitelistInfoRecord1, whitelistInfoRecord);
                        whitelistInfoRecord.setWhitelistInfoRecordId(whitelistInfoRecordId);
                        CarBanDownControlHandler.infoRecords.remove(stationaryCarRecord.getUuid());
                    }
                }
                whitelistInfoRecord.setCardId(stationaryCarRecord.getCardId());
                whitelistInfoRecord.setDealTag(true);
                whitelistInfoRecordService.updateById(whitelistInfoRecord);

            }
        } catch (ParseException e) {
            log.error("数据格式不合法，解析失败");
            throw new JsonParseException("数据格式不合法，解析失败");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
